/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package webClient;

/**
 *
 * @author User
 */

import engine.Singleton;
import figure.Cross;
import figure.Round;
import java.text.ParseException;
import java.util.List;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
import server.ConnectClient;
import tictactoe.BeforeGame;
import tictactoe.EndGame;
import tictactoe.Offer;
import tictactoe.SmallField;
import tictactoe.UserComman;
import tictactoe.noGame;

public class WebClientProtocol {
  WebConnectClient connectClient;
 
  public WebClientProtocol (WebConnectClient connectClient)
  {
    this.connectClient = connectClient;
  }
  
    public void getClient(String str) throws org.json.simple.parser.ParseException, Throwable
   {
       String type;
       JSONParser parser = new JSONParser();
       Object obj = parser.parse(str);
       JSONObject jsonObj = (JSONObject) obj;
       
       type = (String) jsonObj.get("Type");
       
       if (type.equals("Users"))
       {
         //         JSONObject jsonObserv = (JSONObject)jsonObj.get("Data");
         String name = (String) jsonObj.get("Name");
         System.out.println(name);
         connectClient.sok.writeResponse(WebPages.listOfPlaer());
         //addUser(name);
       }
       
       if (type.equals("Offer"))
       {
         String name = (String) jsonObj.get("Name");
         offerUser(name);
       }
       
       if (type.equals("NoGame"))
       {
         noGame();
       }
       
       if (type.equals("StartGame"))
       {
         String figure = (String) jsonObj.get("Figure");
         String step = (String) jsonObj.get("Step");
         startGame(figure, step);
       }
       
       if (type.equals("Step"))
       {
         String row = (String) jsonObj.get("Row");
         String col = (String) jsonObj.get("Col");
         partnerStep(row, col);
       }
       
       if (type.equals("Finish"))
       {
         String status = (String) jsonObj.get("Status");
         finishGame(status);
       }

   }
    
    public void finishGame(String status)
    {
      String end = "1";
      if (status.equals("win"))
        end = "Победил!";
      if (status.equals("lose"))
        end = "Проиграл!";
      if (status.equals("eq"))
        end = "Ничья!";
    
    //  Singleton.gameStatus = "Вы " + end;
    new EndGame(end).setVisible(true);
    UserComman.clearField();
    }
    
    public void partnerStep(String row, String col)
    {
      int r = Integer.parseInt(row);
      int c = Integer.parseInt(col);
      Singleton.sok.steps[r][c] = true;
      SmallField now = Singleton.sok.matrix[r][c];
      if (Singleton.sok.fig){
        now.add(new Round());
        now.repaint();
        Singleton.sok.step = true;
      }
      else{
        now.add(new Cross());
        now.repaint();
        Singleton.sok.step = true;
      }
      UserComman.disblock();
     
    }
    
    public void startGame(String figure, String step)
    {
      boolean fig = false;
      boolean fstep = false;
      if(figure.equals("Cross"))
        fig = true;
      if(step.equals("First"))
        fstep = true;
      UserComman.play(fig, fstep);
     // Singleton.gameStatus = "Ваша фигура "+ figure +" ваш ход " + step;
      new BeforeGame(fig, fstep).setVisible(true);
    }
    
    public void noGame()
    {
      new noGame().setVisible(true);
   //   Singleton.gameStatus = "Вам отказано в игре";
    }
    
    public void offerUser(String name)
    {
      //new Offer(name).setVisible(true);
    //  Singleton.gameStatus = "Вас приглашает поиграть" + name;
      connectClient.setPartner(name);
    }
    
    public void addUser(String name) throws Throwable
    {
      Singleton.Clientusers.clear();
      while (name.length() > 1)
      {
        int end = name.indexOf("-");
        String na = "";
        if (end > 0)
        {
          na = name.substring(0, end);
          name = name.substring(end + 1);
        } else
        {
          na = name;
          name = "";
        }
        Singleton.Clientusers.add(na);
        System.out.println("hh" + na);
      }
      connectClient.sok.writeResponse("пришли имена");
    }
  
}
